using System.Collections.ObjectModel;
using System.Windows;
using HostComputerPlatform.Models;
using System.Linq;

namespace HostComputerPlatform.Views
{
    public partial class VariableLibraryWindow : Window
    {
        public ObservableCollection<Variable> VariableLibrary { get; set; }
        public ObservableCollection<string> VariableTypes { get; set; } = new ObservableCollection<string> { "int", "float", "string", "bool", "array", "object" };

        public VariableLibraryWindow(ObservableCollection<Variable> variableLibrary)
        {
            InitializeComponent();
            VariableLibrary = variableLibrary;
            DataContext = this;
        }

        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            // 可在此处做校验
            this.DialogResult = true;
        }

        private void AddVariable_Click(object sender, RoutedEventArgs e)
        {
            VariableLibrary.Add(new Variable { Name = "", Type = VariableTypes.First(), Value = "", Description = "" });
        }

        private void DeleteVariable_Click(object sender, RoutedEventArgs e)
        {
            var selected = VariableGrid.SelectedItems.Cast<Variable>().ToList();
            if (selected.Count == 0)
            {
                System.Windows.MessageBox.Show("请先选择要删除的变量。", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
                return;
            }
            var result = System.Windows.MessageBox.Show($"确定要删除选中的{selected.Count}个变量吗？", "确认删除", MessageBoxButton.YesNo, MessageBoxImage.Warning);
            if (result == MessageBoxResult.Yes)
            {
                foreach (var v in selected.ToList())
                {
                    VariableLibrary.Remove(v);
                }
            }
        }
    }
} 